package com.zf.shuashua.ui.adapter;

import android.animation.ValueAnimator;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.aliyun.svideo.base.utils.FastClickUtil;
import com.aliyun.svideo.base.widget.beauty.listener.OnViewClickListener;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseViewHolder;
import com.meis.base.mei.adapter.BaseMultiAdapter;
import com.meis.base.mei.utils.NumberUtils;
import com.zf.shuashua.R;
import com.zf.shuashua.bean.comment.CommentEntity;

import java.util.ArrayList;

public class CommentAdapter extends BaseMultiAdapter<CommentEntity> {

    public static final int TYPE_LEVEL_1 = 0;
    public static final int TYPE_LEVEL_2 = 1;
    // 展开更多
    public static final int TYPE_EXPAND_MORE = 2;

    public static final int TYPE_EMPTY = 3;

    private OnViewClickListener mListener;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     * <p>
     * data A new list is created out of this one to avoid mutable list
     */
    public CommentAdapter(OnViewClickListener listener) {
        super(new ArrayList<>());
        this.mListener = listener;
        addItemType(TYPE_LEVEL_1, R.layout.item_comment_level0);
        addItemType(TYPE_LEVEL_2, R.layout.item_comment_level1);
        addItemType(TYPE_EMPTY, R.layout.item_comment_empty);
        addItemType(TYPE_EXPAND_MORE, R.layout.item_comment_expand_more);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, CommentEntity item) {
        super.convert(helper, item);
        switch (item.itemType) {
            case TYPE_LEVEL_1:
            case TYPE_LEVEL_2:

                ImageView ivAvatar = helper.getView(R.id.avatar);

                final ImageView ivPraise = helper.getView(R.id.iv_praise);

                ivPraise.setImageResource(item.is_praise_user ? R.mipmap.comment_praise_selected : R.mipmap.comment_praise_normal);

                Glide.with(mContext)
                        .load(item.user != null ? item.user.avatar : "")
                        .placeholder(R.drawable.changdou_reound_logo)
                        .error(R.drawable.changdou_reound_logo)
                        .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                        .into(ivAvatar);

                helper.setText(R.id.name, item.user != null ? item.user.name : "")
                        .setText(R.id.comment, getContent(item.itemType, item))
                        .setText(R.id.tv_time, item.created_time)
                        .setText(R.id.tv_praise_number, NumberUtils.INSTANCE.convertOneDecimalW(item.praise_count));

                helper.getView(R.id.praise_layout).setOnClickListener(v -> {
                    mListener.onPraise(v, item.itemType == TYPE_LEVEL_2, item.id, helper.getAdapterPosition());

                    ValueAnimator scaleAnim = ValueAnimator.ofFloat(1.2f, 0.8f, 1.1f, 0.9f, 1.0f);
                    scaleAnim.setDuration(600);
                    scaleAnim.addUpdateListener(animation -> {
                        float value = (float) animation.getAnimatedValue();
                        ivPraise.setScaleX(value);
                        ivPraise.setScaleY(value);
                    });
                    scaleAnim.start();
                });

                helper.itemView.setOnLongClickListener(v -> {
                            mListener.onLong(v, item.itemType == TYPE_LEVEL_2, item.user_id + "", item.id, helper.getAdapterPosition(), item.content);
                            return false;
                        }
                );

                helper.itemView.setOnClickListener(v -> {
                    String id = item.id;
                    if (item.itemType == TYPE_LEVEL_2) {
                        id = item.parentIdHelper;
                    }
                    mListener.onReply(v, item.user_id + "", id, item.user == null ? "" : item.user.name, item.itemType == TYPE_LEVEL_2, helper.getAdapterPosition());
                });

                break;
            case TYPE_EXPAND_MORE:
                helper.itemView.setOnClickListener(v -> {
                    if (FastClickUtil.isFastClick()) {
                        return;
                    }
                    int page = item.currentPageHelper;
                    mListener.onLoadMore(v, item.id, page, getLimit(page), getFilterId(helper.getAdapterPosition() - getHeaderLayoutCount()), helper.getAdapterPosition());
                });
                break;
        }
    }

    public int getLimit(int page) {
        return 5;
        // return page <= 1 ? 3 : 10;
    }

    /**
     * @return 显示一条二级回复
     */
    private String getFilterId(int position) {
        position -= 1;
        if (getData().size() > position && position >= 0) {
            return getData().get(position).id;
        }
        return "";
    }

    /**
     * @param item
     */
    public SpannableString getContent(int itemType, CommentEntity item) {
        if (itemType == TYPE_LEVEL_2) {
            if (item.to_user != null) {
                String toUserName = item.to_user.name;
                if (TextUtils.isEmpty(toUserName)) {
                    toUserName = "";
                }
                String spanStr = "回复 " + toUserName + "：" + item.content;
                SpannableString span = new SpannableString(spanStr);
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#999999"));
                span.setSpan(colorSpan, 3, 3 + toUserName.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                return span;
            }
        }
        return new SpannableString(item.content);
    }

    public interface OnViewClickListener {
        void onPraise(View view, boolean isLevel2Reply, String id, int position);

        void onLong(View view, boolean isLevel2Reply, String userId, String id, int position, String content);

        /**
         * @param view
         * @param userId
         * @param id
         * @param userName
         * @param isLevel2Reply 是否二级回复
         * @param position
         */
        void onReply(View view, String userId, String id, String userName, boolean isLevel2Reply, int position);

        /**
         * 加载更多回复评论
         *
         * @param view
         * @param id
         * @param page
         * @param limit
         * @param filterId 第一页 需要过滤已经加载的Item
         * @param position
         */
        void onLoadMore(View view, String id, int page, int limit, String filterId, int position);
    }
}
